package com.yst.gateway.monitor.controllers;

import java.util.Map;
import javax.validation.groups.Default;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.common.validator.groups.Groups;
import com.yst.gateway.monitor.models.QzApplication;
import com.yst.gateway.monitor.services.QzAppsService;

@RestController
@RequestMapping(value = "/qz/apps")
public class QzAppsController {

	@Autowired
	private QzAppsService qzAppsService;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public Response getAll(@RequestParam Map<String, Object> map) {
		Response response = new Response();
		response.success(qzAppsService.getByMap(map));
		return response;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response add(@RequestBody @Validated({Default.class,Groups.Add.class}) QzApplication qzapp) {
		Response response = new Response();
		qzapp.setAppId(UUIDGenerator.getUUID());
		if(qzAppsService.add(qzapp)){
			response.success(qzapp.getAppId()); 
              
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Response get(@PathVariable("id") String id) {
		Response response = new Response();
		response.success(qzAppsService.getById(id));
		return response;
	}
	
	@RequestMapping(value = "/modify", method = RequestMethod.PUT)
	public Response put(@RequestBody @Validated({Default.class,Groups.Modify.class}) QzApplication qzapp) {
		Response response = new Response();
		response.success(qzAppsService.update(qzapp));
		return response;
	}
	
	@RequestMapping(value = "/del/{id}", method = RequestMethod.PUT)
	public Response del(@PathVariable("id") String id) {
		Response response = new Response();
		boolean flag = qzAppsService.delById(id);
		if(flag) {
			response.success();
		}else {
			response.failure();
		}
		return response;
		
	}
	
	@RequestMapping(value = "/lock/{id}",method = RequestMethod.PUT)
	public Response lock(@PathVariable("id") String id){
		Response response=new Response();
		if(qzAppsService.updateStatusById(id)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
	public Response getByPage (@RequestParam Map<String ,Object> paramMap) {
		Response response = new Response();
		int pageNum = 1;
		int pageSize = 10;
		try{
			if(paramMap.containsKey("pageNum")){
				pageNum=Integer.parseInt((String)paramMap.get("pageNum"));
				paramMap.remove("pageNum");
			}
			if(paramMap.containsKey("pageSize")){
				pageSize=Integer.parseInt((String)paramMap.get("pageSize"));
				paramMap.remove("pageSize");
			}
		} catch (Exception e) {
				
			}
		response.success(qzAppsService.getByPage(pageNum, pageSize, paramMap));
		return response;
	}
	
	/**
	 * check if name exists. if id is null, check all records,or check all except which id = parmId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/checkName", method = RequestMethod.GET)
	public boolean checkName(@RequestParam Map<String, String> map) {
		return qzAppsService.checkName(map)==null?true:false;
	}
	
	/**
	 * check if code exists. if id is null, check all records,or check all except which id = parmId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/checkCode", method = RequestMethod.GET)
	public boolean checkCode(@RequestParam Map<String, String> map) {
		return qzAppsService.checkCode(map)==null?true:false;
	}
	
	/**
	 * get Relate interfaces
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getInters/{id}", method = RequestMethod.GET)
	public Response getInters(@PathVariable("id") String id) {
		Response response = new Response();
		response.success(qzAppsService.getIntersById(id));
		return response;
	}
	
	@RequestMapping(value = "/getMatchInters/", method = RequestMethod.PUT)
	public Response getMatchInters(@RequestBody Map<String, Object> map){
		Response response = new Response();
		response.success(qzAppsService.getMatchInters(map));
		return response;
	}
	
	@RequestMapping(value = "/associateInter/", method = RequestMethod.PUT)
	public Response associateInter(@RequestBody Map<String, Object> map){
		Response response = new Response();
		response.success(qzAppsService.associateInter(map));
		return response;
	}
	
	@RequestMapping(value = "/assoDel/{id}", method = RequestMethod.PUT)
	public Response assoDel(@PathVariable("id") String id) {
		Response response = new Response();
		boolean flag = qzAppsService.assoDel(id);
		if(flag) {
			response.success();
		}else {
			response.failure();
		}
		return response;
		
	}
}
